set maxvar 32767

**Combine datasets
use "/Users/survey.dta"
recode oslaua_protect code 
merge m:m code using "/Users/census_cps.dta" 
drop if _merge==2
drop _merge

merge m:1 code using "/Users/code_area.dta"
drop if _merge==2
drop _merge

merge m:1 Area using "/Users/finalpub.dta"
drop _merge

merge m:1 Area using  "/Users/nbpubs.dta"
drop _merge

rename code ID_code
merge m:1 ID_code using "/Users/imd.dta"

**Ratio/pubs per year
gen pub_community=.
replace pub_community = howmany2013community/pubs if year==2013
replace pub_community = howmany2014community/pubs if year==2014
replace pub_community = howmany2015community/pubs if year==2015
replace pub_community = howmany2016community/pubs if year==2016

gen log= log(pop)

sort pid year
quietly by pid year:  gen dup = cond(_N==1,0,_n)
sum dup
tab dup if dup==0
drop if dup==0

** Entropy Balancing (table 10)
ssc install ebalance, all replace
ebalance  gender ethnicity education , manual(0.527 0.738 2.330)
ebalance  gender ethnicity education , manual(0 0 3.5)
xtlogit support pub_community i.year i.wave i.region1 _webal, i(pidp) cluster(ID_code)
xtlogit support pub_community i.gender i.ethnicity i.education age i.year i.wave i.region1 _webal, i(pidp) vce(cluster ID_code)

** With Assets of Community Value (table 12) 
drop _merge
merge m:1 Area using "/Users/ACV.dta"

gen ACVpub=.
replace ACVpub=0 if ACVpubs==0
replace ACVpub=1 if ACVpubs>0
logit support pub_community i.gender i.ethnicity i.education age i.job_3 i.wave EU_Migrantgrowth NONEU_Migrantgrowth ResidentAge60plusshare umemployment_rate_aps imdaveragescore shareofstudents log x_indinub_lw i.ACVpub ,  vce(cluster ID_code) 

** Neighborhood Degradation (table 11)
use "/Users/SCoRe.dta", clear
merge 1:1 serial using "/Users/ward_level.dta"

rename localauthority Area 
merge m:1 Area using "/Users/finalpub.dta"
drop _merge

merge m:1 Area using  "/Users/nbpubs.dta"
drop _merge

recode a16_07 (1=0) (2/11=1), gen(vote_UKIP)
recode a15 (7 8=1) (1 2 3 4 5 6 9 10=0) (11 12 13=.), gen(vote_UKIP_BNP)
encode a50,gen(referendum)
recode referendum (2=1) (3=0) (1=.), gen(Brexit)

** 2017 election : 1=UKIP
encode a52, gen(vote2017)
recode vote2017 (1=0) (5/9=0) (10=1) (11=0) (12=.)

recode a42 (1=0) (2=1) (3=0.5) (4=.), gen(neighborhood_declined)
logit vote_UKIP_BNP neighborhood_declined, or robust

recode a40a_04 (1 2=1) (3 4=0) (5=.), gen(access_culture)
recode a40a_01 (1 2=1) (3 4=0) (5=.), gen(access_postal_services)
recode a40a_02 (1 2=1) (3 4=0) (5=.), gen(access_banks)
recode a40a_03 (1 2=1) (3 4=0) (5=.), gen(access_public_transport)
recode a40a_05 (1 2=1) (3 4=0) (5=.), gen(access_recreational)
recode a40a_06 (1 2=1) (3 4=0) (5=.), gen(access_schools)
recode a40a_07 (1 2=1) (3 4=0) (5=.), gen(access_hospitals)
recode a40a_08 (1 2=1) (3 4=0) (5=.), gen(access_GP)
recode a40b (5=.), gen(urbanity)

recode b22 (1 2 4=1) (3 5/18=0), gen(white_ethnicity)
encode b11b, gen(manual_work)
recode manual_work (1=.) (2=1) (3=0)
encode b11a, gen(job)
recode job (1=.) 

//Education : 0=university degree and +, 1=A-Levels or equivalent, 2=GCSE or equivalent, 3=vocational training and no formal qualifications
recode b03 (1 2 3 4 5 6=0) (7 8 9=1) (10 11 12 13 14=2) (15 16 17 18 19=3), gen(education)
recode a13  (1/3=1) (4/7=0) (8=.), gen(imm_economy)

encode ward, gen(ward1)
xtset ward1
xtlogit vote_UKIP c.neighborhood_declined i.white_ethnicity i.gender i.education age i.urbanity i.job i.imm_economy  , fe or vce(bootstrap)
outreg2 using Table1.xls, nolabel bracket bdec(3) addtext(Ward FE, YES) replace
xtlogit vote_UKIP c.neighborhood_declined##i.white_ethnicity i.gender i.education age i.urbanity i.job i.imm_economy , fe or  vce(bootstrap)
outreg2 using Table1.xls, nolabel bracket bdec(3) addtext(Ward FE, YES) append
xtlogit vote_UKIP c.neighborhood_declined##i.job i.white_ethnicity i.gender i.education age i.urbanity i.imm_economy , fe or  vce(bootstrap)
outreg2 using Table1.xls, nolabel bracket bdec(3) addtext(Ward FE, YES) append
xtlogit vote_UKIP_BNP c.neighborhood_declined i.job i.white_ethnicity i.gender i.education age i.urbanity i.imm_economy , fe or  vce(bootstrap)
outreg2 using Table1.xls, nolabel bracket bdec(3) addtext(Ward FE, YES) append
xtlogit Brexit c.neighborhood_declined i.white_ethnicity i.gender i.education age i.urbanity i.job i.imm_economy , fe or vce(bootstrap)
outreg2 using Table1.xls, nolabel bracket bdec(3) addtext(Ward FE, YES) append
